using System;

namespace Voodoo.Sauce.Internal.Analytics
{
	public class AnalyticsEventTimer
	{
		private enum TimerStatus
		{
			Started = 0,
			Stopped = 1,
			Paused = 2
		}

		private DateTime? _startTime;

		private DateTime? _pauseTime;

		private TimeSpan _pauseDuration = TimeSpan.Zero;

		private TimeSpan _duration = TimeSpan.Zero;

		private bool? _pauseStatus;

		private TimerStatus? _timerStatus;

		public void Start()
		{
			_timerStatus = TimerStatus.Started;
			_startTime = DateTime.Now;
			_pauseTime = null;
			_pauseDuration = TimeSpan.Zero;
			_duration = TimeSpan.Zero;
		}

		public void Stop()
		{
			_timerStatus = TimerStatus.Stopped;
			DateTime now = DateTime.Now;
			if (_startTime.HasValue)
			{
				DateTime value = now;
				DateTime? startTime = _startTime;
				if (value > startTime)
				{
					value = now;
					startTime = _startTime;
					_duration = (value - startTime).Value - _pauseDuration;
					goto IL_0098;
				}
			}
			_duration = TimeSpan.Zero;
			goto IL_0098;
			IL_0098:
			_pauseDuration = TimeSpan.Zero;
			_pauseTime = null;
			_startTime = null;
		}

		public void Pause()
		{
			if (_timerStatus == TimerStatus.Started)
			{
				_timerStatus = TimerStatus.Paused;
				_pauseTime = DateTime.Now;
			}
		}

		public void Resume()
		{
			if (_timerStatus == TimerStatus.Paused && _pauseTime.HasValue)
			{
				_timerStatus = TimerStatus.Started;
				DateTime now = DateTime.Now;
				DateTime value = now;
				DateTime? pauseTime = _pauseTime;
				if (value > pauseTime)
				{
					TimeSpan pauseDuration = _pauseDuration;
					value = now;
					pauseTime = _pauseTime;
					_pauseDuration = pauseDuration + (value - pauseTime).Value;
				}
			}
		}

		public int GetDuration()
		{
			return (int)GetDurationTimeSpan().TotalMilliseconds;
		}

		private TimeSpan GetDurationTimeSpan()
		{
			if (!(_duration.TotalMilliseconds >= 0.0))
			{
				return TimeSpan.Zero;
			}
			return _duration;
		}
	}
}
